<?php
/**
 * Created by 云中有鹿.
 * User: 陈云
 * Blog: http://blog.likecy.cn
 * Date: 2019-11-16
 * Time: 18:37
 */

namespace app\index\controller;


use app\admin\model\kaoq\Course;
use app\common\controller\Api;
use Exception;
use GatewayClient\Gateway;
use GatewayWorker\Lib\Db;
use think\Cache;

class Qiandao extends Api
{
    protected $noNeedLogin =['test','startLearn','closeLearn'];

    public function startLearn(){

        $course_Id = input('post.course_id');

        $course_data = Course::get($course_Id);
        try{
            Gateway::$registerAddress ="127.0.0.1:1236";
            $status = Gateway::isUidOnline($course_data['pid']);
            if($status){
                $data = [
                    'type'=>'startup',
                    'course_id'=>$course_Id
                ];
                $data =json_encode($data);
                Gateway::sendToUid($course_data['pid'],$data);
                return $this->success('上课开始');
            }else{
                return $this->error('设备不在线');
            }
        }catch (Exception $e){
//            return $this->error('php后台还未开启服务哟');
            throw $e;
        }

    }


    public function closeLearn(){
        $course_Id = input('post.course_id');
        $course_data = Course::get($course_Id);
        try {
            Gateway::$registerAddress = "127.0.0.1:1236";
            $status = Gateway::isUidOnline($course_data['pid']);
            if ($status) {
        
                $data = [
                    'type' => 'close',
                    'course_id' => $course_Id
                ];
                $data = json_encode($data);
                Gateway::sendToUid($course_data['pid'], $data);
                return $this->success('关闭成功');
            } else {
                return $this->error('设备不在线');
            }
        }catch (Exception $e){
//            return $this->error('php后台还未开启服务哟');
            throw $e;
        }
    }


    public function test(){

//        $a = exec("php start.php start", $out, $status);
//        print_r($a);
//        print_r($out);
//        print_r($status);
        $res =  Cache::clear();
        return $this->success($res);
//        system('sh ./start.sh',$res);
//        echo $res;
    }



}